package br.edu.fanor.processabatch.main;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class Processa {
	
	
	public static void main(String[] args) throws IOException, InterruptedException {
		
		Long inicio = System.nanoTime();
		
		String pathFile1 = "dados1.txt";
		
		FileWriter fw1 = new FileWriter(new File(pathFile1));
		
		String pathFile2 = "dados2.txt";
		
		FileWriter fw2 = new FileWriter(new File(pathFile2));
		
		String pathFile3 = "dados3.txt";
		
		FileWriter fw3 = new FileWriter(new File(pathFile3));
		
		String pathFile4 = "dados4.txt";
		
		FileWriter fw4 = new FileWriter(new File(pathFile4));
		
		String pathFile5 = "dados5.txt";
		
		FileWriter fw5 = new FileWriter(new File(pathFile5));
		
		GeraArquivo ga1 = new GeraArquivo();
		ga1.setFileWriter(fw1);
		GeraArquivo ga2 = new GeraArquivo();
		ga2.setFileWriter(fw2);
		GeraArquivo ga3 = new GeraArquivo();
		ga3.setFileWriter(fw3);
		GeraArquivo ga4 = new GeraArquivo();
		ga4.setFileWriter(fw4);
		GeraArquivo ga5 = new GeraArquivo();
		ga5.setFileWriter(fw5);
		
		Thread t1 = new Thread(ga1);
		Thread t2 = new Thread(ga2);
		Thread t3 = new Thread(ga3);
		Thread t4 = new Thread(ga4);
		Thread t5 = new Thread(ga5);
		Thread t6 = new Thread(ga1);
		Thread t7 = new Thread(ga2);
		Thread t8 = new Thread(ga3);
		Thread t9 = new Thread(ga4);
		Thread t10 = new Thread(ga5);
		Thread t11 = new Thread(ga1);
		Thread t12 = new Thread(ga2);
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		t6.start();
		t7.start();
		t8.start();
		t9.start();
		t10.start();
		t11.start();
		t12.start();
		
		while (t1.isAlive() || t2.isAlive() || t3.isAlive() || t4.isAlive()
				 || t5.isAlive() || t6.isAlive() || t7.isAlive() || t8.isAlive()
				 || t9.isAlive() || t10.isAlive() || t11.isAlive() || t12.isAlive()){
			
		}
		
		fw1.close();
		fw2.close();
		fw3.close();
		fw4.close();
		fw5.close();
		
		Process p = Runtime.getRuntime().exec("processa.bat");
		p.waitFor();
		
		Long tempoTotal = TimeUnit.SECONDS.
				convert((System.nanoTime() - inicio), 
						TimeUnit.NANOSECONDS);
		System.out.println("Tempo Total: "+tempoTotal);
	}	

}
